package com.sdkx.app.fragment;


import android.content.SharedPreferences;
import android.os.Bundle;
import android.app.Fragment;
import android.preference.EditTextPreference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sdkx.app.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class SecuritySetFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener{
    SharedPreferences sharedPreferences;

    public SecuritySetFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_security_set);
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
        init();
    }

    @Override
    public void onStart() {
        super.onStart();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void init(){
        EditTextPreference preference1 = (EditTextPreference)findPreference("duiDiGaoJingBanJing");
        String duiDiGaoJingBanJing = sharedPreferences.getString("duiDiGaoJingBanJing","500");
        preference1.setSummary(duiDiGaoJingBanJing);

        EditTextPreference preference2 = (EditTextPreference)findPreference("anQuanGaoDuCha");
        String anQuanGaoDuCha = sharedPreferences.getString("anQuanGaoDuCha","500");
        preference2.setSummary(anQuanGaoDuCha);

        EditTextPreference preference3 = (EditTextPreference) findPreference("anQuanZheFanYouLiang");
        String anQuanZheFanYouLiang = sharedPreferences.getString("anQuanZheFanYouLiang","500");
        preference3.setSummary(anQuanZheFanYouLiang);

        EditTextPreference preference4 = (EditTextPreference) findPreference("hangXianKuanDu");
        String hangXianKuanDu = sharedPreferences.getString("hangXianKuanDu","500");
        preference4.setSummary(hangXianKuanDu);

        SwitchPreference preference5 = (SwitchPreference) findPreference("jinDiBaoJing");
        boolean jinDiBaoJing = sharedPreferences.getBoolean("jinDiBaoJing",false);
        preference5.setSummary(jinDiBaoJing?"开":"关");

        SwitchPreference preference6 = (SwitchPreference) findPreference("shuiPingPainChaBaoJing");
        boolean shuiPingPainChaBaoJing = sharedPreferences.getBoolean("shuiPingPainChaBaoJing",false);
        preference6.setSummary(shuiPingPainChaBaoJing?"开":"关");

        SwitchPreference preference7 = (SwitchPreference) findPreference("chuiZhiPianChaBaoJing");
        boolean chuiZhiPianChaBaoJing = sharedPreferences.getBoolean("chuiZhiPianChaBaoJing",false);
        preference7.setSummary(chuiZhiPianChaBaoJing?"开":"关");
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
        switch (s) {
            case "duiDiGaoJingBanJing":
                String duiDiGaoJingBanJing = sharedPreferences.getString("duiDiGaoJingBanJing","500");
                findPreference("duiDiGaoJingBanJing").setSummary(duiDiGaoJingBanJing);
                break;
            case "anQuanGaoDuCha":
                String anQuanGaoDuCha = sharedPreferences.getString("anQuanGaoDuCha","500");
                findPreference("anQuanGaoDuCha").setSummary(anQuanGaoDuCha);
                break;
            case "anQuanZheFanYouLiang":
                String anQuanZheFanYouLiang = sharedPreferences.getString("anQuanZheFanYouLiang","500");
                findPreference("anQuanZheFanYouLiang").setSummary(anQuanZheFanYouLiang);
                break;
            case "hangXianKuanDu":
                String hangXianKuanDu = sharedPreferences.getString("hangXianKuanDu","500");
                findPreference("hangXianKuanDu").setSummary(hangXianKuanDu);
                break;
            case "jinDiBaoJing":
                boolean jinDiBaoJing = sharedPreferences.getBoolean("jinDiBaoJing",false);
                findPreference("jinDiBaoJing").setSummary(jinDiBaoJing?"开":"关");
                break;
            case "shuiPingPainChaBaoJing":
                boolean shuiPingPainChaBaoJing = sharedPreferences.getBoolean("shuiPingPainChaBaoJing",false);
                findPreference("shuiPingPainChaBaoJing").setSummary(shuiPingPainChaBaoJing?"开":"关");
                break;
            case "chuiZhiPianChaBaoJing":
                boolean chuiZhiPianChaBaoJing = sharedPreferences.getBoolean("chuiZhiPianChaBaoJing",false);
                findPreference("chuiZhiPianChaBaoJing").setSummary(chuiZhiPianChaBaoJing?"开":"关");
                break;
            default:
                break;
        }
    }
}
